package Aula21;

class ChildrenListNode{
    public BTreeNode child;
    public ChildrenListNode next;
    
	public ChildrenListNode(BTreeNode child, ChildrenListNode next) {
		this.child = child;
		this.next = next;
	}
	
	public ChildrenListNode(BTreeNode child){
		this(child, null);
	}
	
	public ChildrenListNode(){
		super();
	}
	
	public ChildrenListNode copy(){
		ChildrenListNode node = this;
		ChildrenListNode newNode = new ChildrenListNode(node.child);
		ChildrenListNode atual = newNode;
		while(node.next != null){
			atual.next = new ChildrenListNode(node.next.child);
			atual = atual.next;
			node = node.next;
		}
		return newNode;
	}
	
	public int indexOf(BTreeNode child){
		ChildrenListNode node = this;
		int i = 0;
		for(; node != null && node.child != child; i++){
			node = node.next;
		}
		if(node == null)
			return -1;
		return i;
	}
	
	public BTreeNode get(int i){
		ChildrenListNode node = getNode(i);
		if(node == null)
			return null;
		return node.child;
	}
	
	public BTreeNode substituirFilho(BTreeNode newFilho, int i){
		ChildrenListNode node = getNode(i);
		if(node != null){
			BTreeNode filho = node.child;
			node.child = newFilho;
			return filho;
		}
		return null;
	}
	
	public ChildrenListNode getNode(int i){
		ChildrenListNode node = this;
		for(; i > 0; i--){
			node = node.next;
			if(node == null)
				return null;
		}
		return node;
	}
	
	public BTreeNode remove(BTreeNode child){
		ChildrenListNode anterior = null;
		ChildrenListNode atual = this;

		while (atual != null && atual.child != child) {
			anterior = atual;
			atual = atual.next;
		}
		if(atual == null){
			return null;
		}
		if(anterior == null){
			if(atual.next == null)
				System.out.println("Erro na lista de filhos!");
			atual.child = atual.next.child;
			atual.next = atual.next.next;
		}else
			anterior.next = atual.next;
		return child;		
	}
	
	public void add(BTreeNode child){
		last().next = new ChildrenListNode(child);
	}
	
	public void add(ChildrenListNode filho){
		ChildrenListNode node = this;
		while(node.next != null && filho.compareTo(node.next) > 0){
			node = node.next;
		}
		if(node == this && filho.compareTo(node) < 0){
			BTreeNode filhoAtual = child;
			ChildrenListNode nextAtual = next; 
			child = filho.child;
			next = new ChildrenListNode(filhoAtual, nextAtual);
		}else
			node.next = new ChildrenListNode(filho.child, node.next);
	}
	
	public BTreeNode lastChild(){
		ChildrenListNode node = last();
		if(node != null)
			return node.child;
		return null;
	}
	
	public ChildrenListNode last(){
		ChildrenListNode atual = this;
		while (atual.next != null) {
			atual = atual.next;
		}
		return atual;
	}

	public int compareTo(ChildrenListNode o) {
		if(o != null){
			if(child.keys.last() == o.child.keys.last())
				return 0;
			if(child.keys.last() > o.child.keys.last())
				return 1;
		}
		return -1;
	}
	
	
}